//
//  UpdateDatabase.m
//  CI
//
//  Created by tongbu_jinzhongjun on 14-10-20.
//  Copyright (c) 2014年 tongbu_jinzhongjun. All rights reserved.
//

#import "UpdateDatabase.h"
#import <Objection.h>
#import <CoreData+MagicalRecord.h>
#import "Hospital.h"
#import "Company.h"
#import "UserProduct.h"
#import "Type.h"
#import "Product.h"
#import "Test.h"
#import "CompanyProduct.h"

@implementation UpdateDatabase
objection_register_singleton(UpdateDatabase)

-(void)updateHospitalWithDic:(NSDictionary *)dic {

    NSString *find_id = [dic objectForKey:@"hospitalId"];
    NSArray *array = [Hospital MR_findByAttribute:@"hospital_id" withValue:[NSNumber numberWithInt:[find_id intValue]]];
    Hospital *hospital = nil;
    if ([array count] > 0) {
        hospital = [array objectAtIndex:0];
    }else {
        hospital = [Hospital MR_createEntity];
    }
    hospital.address = [dic objectForKey:@"address"];
    hospital.hospital_id = [dic objectForKey:@"hospitalId"];
    hospital.hospital_name = [dic objectForKey:@"name"];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"保存成功");
        }else {
//            NSLog(@"error:%@",error);
        }
    }];
}

-(void)updateTypeWithDic:(NSDictionary *)dic {
    NSString *find_id = [dic objectForKey:@"typeId"];
    NSArray *array = [Type MR_findByAttribute:@"type_id" withValue:find_id];
    Type *type = nil;
    if ([array count] > 0) {
        type = [array objectAtIndex:0];
    }else {
        type = [Type MR_createEntity];
    }
    
    type.type_id = [dic objectForKey:@"typeId"];
    type.type_name = [dic objectForKey:@"name"];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

-(void)updateCompanyWithDic:(NSDictionary *)dic {
    NSString *find_id = [dic objectForKey:@"companyId"];
    NSArray *array = [Company MR_findByAttribute:@"company_id" withValue:find_id];
    Company *company = nil;
    if ([array count] > 0) {
        company = [array objectAtIndex:0];
    }else {
        company = [Company MR_createEntity];
    }
    
    company.company_id = [dic objectForKey:@"companyId"];
    company.company_name = [dic objectForKey:@"name"];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    
    NSArray*productArray = [dic objectForKey:@"product"];
    for (NSDictionary *temp_dic in productArray) {
        [self updateProductWithDic:temp_dic withCompanyId:find_id];
    }
}

-(void)updateProductWithDic:(NSDictionary *)dic withCompanyId:(NSString *)companyId {
    
    NSString *find_id = [dic objectForKey:@"productId"];
    NSArray *array = [Product MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"product_id == %d && company_id == %d",find_id.intValue,companyId.intValue]]; //[Product MR_findByAttribute:@"product_id" withValue:find_id];
    Product *product = nil;
    if ([array count] > 0) {
        product = [array objectAtIndex:0];
    }else {
        product = [Product MR_createEntity];
    }
    
    product.product_id = [dic objectForKey:@"productId"];
    product.company_id = [NSNumber numberWithInt:companyId.intValue];
    product.product_name = [dic objectForKey:@"name"];
    product.count = [dic objectForKey:@"count"];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

}

-(void)updateUserProductWithDic:(NSDictionary *)dic {
    NSString *find_id = [dic objectForKey:@"userProductId"];
    NSArray *array = [UserProduct MR_findByAttribute:@"user_product_id" withValue:find_id];
    UserProduct *product = nil;
    if ([array count] > 0) {
        product = [array objectAtIndex:0];
    }else {
        product = [UserProduct MR_createEntity];
    }
    
    product.user_product_id = [dic objectForKey:@"userProductId"];
    product.company_id = [dic objectForKey:@"companyId"];
    product.hospital_id = [dic objectForKey:@"hospitalId"];
    product.user_id = [dic objectForKey:@"userId"];
    product.type_id = [dic objectForKey:@"typeId"];
    product.count = [dic objectForKey:@"count"];
    product.product_id = [dic objectForKey:@"productId"];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    
}


-(void)deleteAllUserData {
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1 == 1"];
        
    [Company MR_deleteAllMatchingPredicate:predicate];
    [CompanyProduct MR_deleteAllMatchingPredicate:predicate];
    [Hospital MR_deleteAllMatchingPredicate:predicate];
    [Product MR_deleteAllMatchingPredicate:predicate];
    [Type MR_deleteAllMatchingPredicate:predicate];
    [UserProduct MR_deleteAllMatchingPredicate:predicate];
}

@end
